home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_087 / sc / sc.c < prev   
C/C++ Source or Header  |  1992-05-06  |  6KB  |  211 lines

  1. #include "intuition/intuition.h"
  2. #include "libraries/dos.h"
  3.  
  4. #define FREEPEN (-1)
  5.  
  6. struct Screen *OpenScreen();
  7. struct Window *OpenWindow();
  8.  
  9. /*
  10.  * sc.c - fractalish terrain generator.
  11.  *
  12.  *   Date:     March 5, 1987 (original version sometime in 1985)
  13.  *   Author:   Chris Gray
  14.  *   Language: C
  15.  *   System:   Amiga
  16.  */
  17.  
  18. /*
  19.  * The nature of the terrain can be changed by playing with the numbers
  20.  * in 'Range'. If you change SIZE, you must also change the number of
  21.  * values given for 'Range'. The created terrain with SIZE = 8 is 256 pixels
  22.  * by 256 pixels, which doesn't fit on a non-interlaced screen, so only the
  23.  * top 200 pixels are displayed. The terrain is a torus, i.e. wraps around
  24.  * both vertically and horizontally.
  25.  */
  26.  
  27. /*
  28.  * Feel free to use this algorithm in any games you write, so long as you
  29.  * give me credit for it. (I THINK I invented it, since I've never heard of
  30.  * anything similar, and other programs I've seen use much slower methods.)
  31.  */
  32.  
  33. #define SIZE 8
  34. #define COUNT (1 << SIZE)
  35. #define SCREEN_WIDTH 320
  36. #define SCREEN_HEIGHT 200
  37. #define SCREEN_DEPTH 5
  38. #define COLOURS (1 << SCREEN_DEPTH)
  39. #define WINDOW_WIDTH (COUNT < SCREEN_WIDTH ? COUNT : SCREEN_WIDTH)
  40. #define WINDOW_HEIGHT (COUNT < SCREEN_HEIGHT ? COUNT : SCREEN_HEIGHT)
  41.  
  42. unsigned short Range[SIZE] = {32, 32, 32, 22, 14, 8, 4, 2};
  43.  
  44. unsigned short ColourMap[COLOURS] = {
  45.     0x00f, 0x050, 0x070, 0x0a0, 0x0c0, 0x0e0, 0x4f4, 0x8f8,
  46.     0xdf0, 0xff0, 0xfd0, 0xfb0, 0xf90, 0xf70, 0xe50, 0xe33,
  47.     0xe11, 0xf01, 0xf03, 0xf05, 0xf07, 0xf09, 0xf0b, 0xf0d,
  48.     0xf1f, 0xf3f, 0xf5f, 0xf7f, 0xf9f, 0xfbf, 0xfdf, 0xfff
  49. };
  50.  
  51. struct Screen *Screen;
  52. struct Window *Window;
  53.  
  54. unsigned short Seed;
  55.  
  56. short Cell[COUNT][COUNT];
  57.  
  58. /*
  59.  * random - return a random number 0 - passed range.
  60.  */
  61.  
  62. unsigned short random(rang)
  63. unsigned short rang;
  64. {
  65.     if (rang == 0)
  66.         return 0;
  67.     Seed = Seed * 17137 + 4287;
  68.     Seed = (Seed >> 8) ^ (Seed << 8);
  69.     return Seed % rang;
  70. }
  71.  
  72. /*
  73.  * set - set a given spot in Cell.
  74.  */
  75.  
  76. void set(l, c, size, height)
  77. unsigned short l, c, size;
  78. short height;
  79. {
  80.     unsigned short rang;
  81.  
  82.     rang = Range[size];
  83.     height = height + random(rang) - (rang + 1) / 2;
  84.     Cell[l][c] = height;
  85. }
  86.  
  87. /*
  88.  * grow - grow the basic scenery heights.
  89.  */
  90.  
  91. void grow()
  92. {
  93.     unsigned short l, c, i, step, nextStep, l1, l2, c1, c2;
  94.  
  95.     Cell[0][0] = 0;
  96.     step = COUNT;
  97.     for (i=0; i<SIZE; i++)
  98.         {
  99.         nextStep = step / 2;
  100.         for (l=0; l<COUNT; l+=step)
  101.             {
  102.             l1 = l + nextStep;
  103.             l2 = l + step;
  104.             if (l2 == COUNT)
  105.                 l2 = 0;
  106.             for (c=0; c<COUNT; c+=step)
  107.                 {
  108.                 c1 = c + nextStep;
  109.                 c2 = c + step;
  110.                 if (c2 == COUNT)
  111.                     c2 = 0;
  112.                 set(l, c1, i, (Cell[l][c] + Cell[l][c2] + 1) / 2);
  113.                 set(l1, c, i, (Cell[l][c] + Cell[l2][c] + 1) / 2);
  114.                 set(l1, c1, i, (Cell[l][c] + Cell[l][c2] +
  115.                                  Cell[l2][c] + Cell[l2][c2] + 2) / 4);
  116.                 }
  117.             }
  118.         step = nextStep;
  119.         }
  120. }
  121.  
  122. /*
  123.  * display - display the resulting scenery.
  124.  */
  125.  
  126. void display()
  127. {
  128.     unsigned short l, c;
  129.     short height;
  130.  
  131.     for (l=0; l<WINDOW_HEIGHT; l++)
  132.         {
  133.         for (c=0; c<WINDOW_WIDTH; c++)
  134.             {
  135.             height = Cell[l][c];
  136.             SetAPen(Window->RPort,
  137.                     height<0 ?
  138.                         0
  139.                     : (height>=COLOURS ?
  140.                         COLOURS-1
  141.                     : height));
  142.             WritePixel(Window->RPort, c, l);
  143.             }
  144.         }
  145. }
  146.  
  147. struct IntuitionBase *IntuitionBase;
  148. struct GfxBase       *GfxBase;
  149. /*
  150.  * main program.
  151.  */
  152.  
  153. void main()
  154. {
  155.     static struct NewWindow newWindow =
  156.             {
  157.             0, 0, WINDOW_WIDTH, WINDOW_HEIGHT,
  158.             FREEPEN, FREEPEN,
  159.             0x0, BORDERLESS | ACTIVATE | NOCAREREFRESH,
  160.             NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0,
  161.             CUSTOMSCREEN
  162.             };
  163.  
  164.     static struct NewScreen newScreen =
  165.           {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, 0, 1,
  166.           0x0, CUSTOMSCREEN, NULL, NULL, NULL, NULL};
  167.     struct DateStamp ds;
  168.     static struct IntuiText bodyText =
  169.         {COLOURS - 1, 0, 0, 51, 5, NULL, NULL, NULL};
  170.     static struct IntuiText positiveText =
  171.         {COLOURS - 1, 0, 0, 7, 3, NULL, NULL, NULL};
  172.     static struct IntuiText negativeText =
  173.         {COLOURS - 1, 0, 0, 7, 3, NULL, NULL, NULL};
  174.  
  175.     bodyText.IText = "Done";
  176.     positiveText.IText = "Next";
  177.     negativeText.IText = "Quit";
  178.     IntuitionBase = (struct IntuitionBase *)
  179.                     OpenLibrary("intuition.library", 0);
  180.     if (IntuitionBase != NULL)
  181.         {
  182.         GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0);
  183.         if (GfxBase != NULL)
  184.             {
  185.                 Screen = OpenScreen(&newScreen);
  186.                 if (Screen != NULL)
  187.                     {
  188.                     LoadRGB4(&Screen->ViewPort, ColourMap, COLOURS);
  189.                     newWindow.Screen = Screen;
  190.                     Window = OpenWindow(&newWindow);
  191.                     if (Window != NULL)
  192.                         {
  193.                         DateStamp(&ds);
  194.                         Seed = (ds.ds_Minute ^ ds.ds_Tick) | 1;
  195.                         do
  196.                             {
  197.                             grow();
  198.                             display();
  199.                             }
  200.                         while (AutoRequest(Window, &bodyText, &positiveText,
  201.                                &negativeText, 0x0, 0x0, 150, 50));
  202.                         CloseWindow(Window);
  203.                         }
  204.                     CloseScreen(Screen);
  205.                     }
  206.             CloseLibrary(IntuitionBase);
  207.             }
  208.         CloseLibrary(GfxBase);
  209.         }
  210. }
  211.